package com.nbjtjc.safe.service.safe;

import cn.hutool.core.collection.CollectionUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.DangerInspectListMapper;
import com.nbjtjc.safe.mapper.danger.DangerInspectListPublicMapper;
import com.nbjtjc.safe.mapper.danger.DangerInspectMapper;
import com.nbjtjc.safe.mapper.danger.DangerInspectPublicMapper;
import com.nbjtjc.safe.model.danger.DangerInspectList;
import com.nbjtjc.safe.model.danger.DangerInspectListPublic;
import com.nbjtjc.safe.model.danger.DangerInspectPublic;
import com.nbjtjc.safe.model.safe.SafeDangerInspectPlan;
import com.nbjtjc.safe.model.safe.SafeDangerInspectTask;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SafeInspectTablePublicServiceImpl {

    @Autowired
    private DangerInspectMapper dangerInspectMapper;

    @Autowired
    private DangerInspectListMapper dangerInspectListMapper;

    @Autowired
    private DangerInspectPublicMapper dangerInspectPublicMapper;

    @Autowired
    private DangerInspectListPublicMapper dangerInspectListPublicMapper;


    public Result publicTables(List<DangerInspectPublic> dangerInspectPublicList, SafeDangerInspectPlan plan, int createUserId,boolean needSetPlanId) {
        Result result = new Result();
        if (CollectionUtil.isNotEmpty(dangerInspectPublicList)) {
            dangerInspectPublicList.forEach(dangerInspectPublic -> {
                if(needSetPlanId){
                    dangerInspectPublic.setInspectPlanId(plan.getId());
                }else{
                    dangerInspectPublic.setInspectPlanId(null);
                }
                dangerInspectPublic.setInspectId(dangerInspectPublic.getId());
                dangerInspectPublic.setId(null);
                dangerInspectPublic.setCreateUserId(createUserId);
                dangerInspectPublic.setCheckUserId(plan.getInspectUserId());
                dangerInspectPublic.setCreateTime(new Date());
                dangerInspectPublicMapper.insertSelective(dangerInspectPublic);
                DangerInspectList inspectList = new DangerInspectList();
                inspectList.setInspectId(dangerInspectPublic.getInspectId());
                List<DangerInspectList> dangerInspectLists =
                        dangerInspectListMapper.select(inspectList);
                List<DangerInspectListPublic> listPublicList = new ArrayList<>();
                //todo mapstruct 类拷贝
                dangerInspectLists.forEach(tableList -> {
                    DangerInspectListPublic inspectListPublic = new DangerInspectListPublic();
                    inspectListPublic.setInspectContent(tableList.getInspectContent());
                    inspectListPublic.setInspectProject(tableList.getInspectProject());
                    inspectListPublic.setSuggestion(tableList.getSuggestion());
                    inspectListPublic.setInspectPublicId(dangerInspectPublic.getId());
                    inspectListPublic.setBasisId(tableList.getBasisId());
                    inspectListPublic.setCreateTime(new Date());
                    inspectListPublic.setActionUserId(plan.getInspectUserId());
                    inspectListPublic.setInspectProjectOrder(tableList.getInspectProjectOrder());
                    listPublicList.add(inspectListPublic);
                });
                if (CollectionUtil.isNotEmpty(listPublicList)) {
                    dangerInspectListPublicMapper.insertList(listPublicList);
                    result.setData(listPublicList);
                }
            });
        }
        return result;
    }

}
